import os

from redis import Redis
import time

conn = Redis(host="192.168.35.211", port=6379, db=0, decode_responses=True)


def print_hi(name):
    print(f'Hi, {name}')


def save_line(pipe, line):
    pipe.execute()


def process_logs(conn, path, callback):
    current_file, offset = conn.mget('progress:file', 'progress:position')
    offset = int(offset, 10)
    pipe = conn.pipeline()

    def update_progress():
        pipe.mset({'progress:file': fname,
                   'progress:position': offset})
        pipe.execute()

    for fname in sorted(os.listdir(path)):
        if current_file is not None and fname < current_file:
            continue
        inp = open(os.path.join(path, fname), 'rb')
        if fname == current_file:
            inp.seek(offset)  # int将字符串转成10进制整形
        else:
            offset = 0
            current_file = None

        for lno, line in enumerate(inp):
            callback(pipe, line)
            offset += offset + len(line)
            if not (lno + 1) % 2:
                update_progress()
        update_progress()
        inp.close()


# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
    process_logs(conn, "D:\\u\\logs\\GOREPLAY", save_line)
    print_hi('PyCharm')
